//
//  AVFile+UploadMoreFile.m
//  
//
//  Created by rimi on 16/7/21.
//
//

#import "AVFile+UploadMoreFile.h"

@implementation AVFile (UploadMoreFile)

+ (void)uploadFiles:(NSArray *)files completion:(Completion)completion
{
    __block  NSInteger count = 0;
    [files enumerateObjectsUsingBlock:^(AVFile *  _Nonnull file, NSUInteger idx, BOOL * _Nonnull stop) {
        [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            count++;
            if(count == files.count || error){
                //出错 或者上传完毕
                if (error) {
                    completion(nil,error);
                }else{
                    NSMutableArray *arr = [NSMutableArray array];
                    [files enumerateObjectsUsingBlock:^(AVFile * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.url) {
                            [arr addObject:obj.url];
                        }
                    }];
                    
                    NSString *str = [arr componentsJoinedByString:@","];
                    completion(str,nil);
                }
                
            }
        }];
    }];
    
}


@end
